/**
 * Created by maiseries on 2018/1/15.
 */
import Request from '../utils/Request'
import Constants from '../config/Constants'

export default class Passport {
    /**
     * 登录
     * @param options
     * @returns {Promise.<*>}
     */
    static async login(options) {
        options.url = "/passport/login";
        try {
            const res = await Request.post(options);
            const data = Request.valid(res);
            await this.saveToken({
                token: data.accessToken,
                user_name: format.phone(options.params.login_name)
            });
            user.token = data.accessToken;
            user.user_name = format.phone(options.params.login_name);
            user.loginState = true;
            return {
                result: true,
                message: '登录成功'
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    /**
     * 登出
     * @returns {Promise.<*>}
     */
    static async logout() {
        let options = {};
        options.url = "/passport/logOut";
        try {
            // const res = await Request.token(options);
            await Request.token(options);
            // Request.valid(res);
            await this.removeToken();
            user.token = '';
            user.loginState = false;
            return {
                result: true,
                message: '退出成功'
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    /**
     * 注册
     * @param options
     * @returns {Promise.<*>}
     */
    static async register(options) {
        options.url = "/passport/register";
        try {
            const res = await Request.post(options);
            const data = Request.valid(res);
            await this.saveToken({
                token: data.accessToken,
                user_name: format.phone(options.params.login_name)
            });
            user.token = data.accessToken;
            user.user_name = format.phone(options.params.login_name);
            user.loginState = true;
            return {
                result: true,
                message: '注册成功'
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    /**
     * 注册发送验证码
     * @param options
     * @returns {Promise.<*>}
     */
    static async registerSend(options) {
        options.url = "/passport/sendRegisterCode";
        try {
            const res = await Request.post(options);
            Request.valid(res);
            return {
                result: true,
                message: '发送成功'
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    /**
     * 修改密码
     * @param options
     * @returns {Promise.<*>}
     */
    static async modify(options) {
        options.url = "/passport/modify";
        try {
            const res = await Request.token(options);
            Request.valid(res);
            await this.removeToken();
            user.token = '';
            user.loginState = false;
            return {
                result: true,
                message: '密码修改成功'
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    /**
     * 修改密码发送验证码
     * @returns {Promise.<*>}
     */
    static async modifySend() {
        let options = {};
        options.url = '/passport/sendModifyCode';
        try {
            const res = await Request.token(options);
            Request.valid(res);
            return {
                result: true,
                message: '发送成功'
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }


    static async forgot(options) {
        options.url = "/passport/resetPwd";
        try {
            const res = await Request.token(options);
            Request.valid(res);
            await this.removeToken();
            user.token = '';
            user.loginState = false;
            return {
                result: true,
                message: '密码修改成功'
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async forgotSend(options) {
        options.url = "/passport/sendResetCode";
        try {
            const res = await Request.post(options);
            const data = Request.valid(res) ? Request.valid(res) : {auth: false};
            return {
                result: true,
                message: '发送成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async saveToken(token) {
        await storage.save({
            key: Constants.ACCESS_TOKEN_STORAGE,
            data: token
        });
    }

    static async removeToken() {
        await storage.remove({
            key: Constants.ACCESS_TOKEN_STORAGE
        })
    }

    static async loadToken() {
        const ret = await storage.load({
            key: Constants.ACCESS_TOKEN_STORAGE,
            // autoSync(默认为true)意味着在没有找到数据或数据过期时自动调用相应的sync方法
            autoSync: false,
            // syncInBackground(默认为true)意味着如果数据过期，
            // 在调用sync方法的同时先返回已经过期的数据。
            // 设置为false的话，则始终强制返回sync方法提供的最新数据(当然会需要更多等待时间)。
            syncInBackground: false,
        });
        return ret;
    }
}